package cn.elwy.rcp.ui.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import cn.elwy.rcp.core.model.ui.tree.TreeElement;

/**
 * 控件的选择、添加、删除等操作工具
 * 
 * @author huangsq
 * @version 1.0, 2013-11-20
 * @since 1.0, 2013-11-20
 */
public class TreeViewerUtil {

  /** 获得TreeViewer中选中的所有节点 */
  public static TreeElement[] getSelectedNodes(TreeViewer treeViewer) {
    IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
    if (selection == null) {
      return null;
    }
    List<TreeElement> selectedNodes = new ArrayList<TreeElement>();
    Iterator<?> it = selection.iterator();
    while (it.hasNext()) {
      Object object = it.next();
      if (object instanceof TreeElement) {
        selectedNodes.add((TreeElement) object);
      }
    }
    if (selectedNodes.size() == 0) {
      return null;
    }
    TreeElement[] nodes = (TreeElement[]) selectedNodes.toArray(new TreeElement[] {});
    return nodes;
  }

  /** 获得TreeViewer中选中的第一个节点 */
  public static TreeElement getSelectedNode(TreeViewer treeViewer) {
    IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
    if (null != selection) {
      return (TreeElement) selection.getFirstElement();
    }
    return null;
  }

  /**
   * 获取所有选项
   * 
   * @param tree 树对象
   * @return
   */
  public static List<TreeItem> getAllTreeItem(Tree tree) {
    List<TreeItem> selectItems = new ArrayList<TreeItem>();
    for (TreeItem treeItem : tree.getItems()) {
      selectItems.add(treeItem);
      if (treeItem.getItems() != null) {
        getAllTreeItem(treeItem, selectItems);
      }
    }
    return selectItems;
  }

  /**
   * 获取所有子选项
   * 
   * @param parentItem 父选项
   * @param selectItems 选中的列表
   * @return
   */
  public static List<TreeItem> getAllTreeItem(TreeItem parentItem, List<TreeItem> selectItems) {
    for (TreeItem treeItem : parentItem.getItems()) {
      selectItems.add(treeItem);
      if (treeItem.getItems() != null) {
        getAllTreeItem(treeItem, selectItems);
      }
    }
    return selectItems;
  }

  /**
   * 获取所有被中的选项
   * 
   * @param tree 树对象
   * @return
   */
  public static List<TreeItem> getSelectTreeItem(Tree tree) {
    List<TreeItem> selectItems = new ArrayList<TreeItem>();
    for (TreeItem treeItem : tree.getItems()) {
      if (treeItem.getChecked()) {
        selectItems.add(treeItem);
      }
      if (treeItem.getItems() != null) {
        getSelectTreeItem(treeItem, selectItems);
      }
    }
    return selectItems;
  }

  /**
   * 获取所有被中的选项
   * 
   * @param parentItem 父选项
   * @param selectItems 选中的列表
   * @return
   */
  public static List<TreeItem> getSelectTreeItem(TreeItem parentItem, List<TreeItem> selectItems) {
    for (TreeItem treeItem : parentItem.getItems()) {
      if (treeItem.getChecked()) {
        selectItems.add(treeItem);
      }
      if (treeItem.getItems() != null) {
        getSelectTreeItem(treeItem, selectItems);
      }
    }
    return selectItems;
  }

  /**
   * 添加复选筐选择事件
   * 
   * @param tree
   */
  public static void addCheckedListener(final Tree tree) {
    Listener checkListener = new Listener() {
      private boolean isCheck = false;

      public void handleEvent(Event event) {
        TreeItem treeItem = null;
        if (event.detail == SWT.CHECK) { // 选中事件
          treeItem = (TreeItem) event.item;
          isCheck = !isCheck;
          treeItem.setChecked(isCheck);
        } else if (event.type == SWT.MouseDown) { // 鼠标单击事件
          treeItem = tree.getItem(new Point(event.x, event.y));
          if (treeItem == null) {
            return;
          }
          if (isCheck == treeItem.getChecked()) {
            isCheck = !isCheck;
          }
          if (!isCheck) {
            tree.setSelection(treeItem);
          }
          treeItem.setChecked(isCheck);
        } else if (event.keyCode == SWT.CHECK) { // 按下空格事件
          treeItem = tree.getSelection()[0];
          if (isCheck != treeItem.getChecked()) {
            isCheck = !isCheck;
          }
          treeItem.setChecked(isCheck);
        }
      }
    };
    tree.addListener(SWT.Selection, checkListener);
    tree.addListener(SWT.MouseDown, checkListener);
    tree.addListener(SWT.KeyDown, checkListener);
  }

}
